home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / ZIPPED / DOS / GRAPHICS / RAYSH386.ZIP / SRC / FBM.C < prev    next >
Text File  |  1992-05-05  |  2KB  |  82 lines

  1. /*
  2.  * fbm.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: fbm.c,v 4.0 91/07/17 14:42:06 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    fbm.c,v $
  19.  * Revision 4.0  91/07/17  14:42:06  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "texture.h"
  24. #include "fbm.h"
  25.  
  26. FBm *
  27. FBmCreate(offset, scale, h, lambda, octaves, thresh, mapname)
  28. Float offset, scale, h, lambda;
  29. int octaves;
  30. Float thresh;
  31. char *mapname;
  32. {
  33.     FBm *fbm;
  34.  
  35.     fbm = (FBm *)RayMalloc(sizeof(FBm));
  36.  
  37.     fbm->beta = 2.0*h + 1.0;
  38.     fbm->lambda = lambda;
  39.     fbm->scale = scale;
  40.     fbm->offset = offset;
  41.     fbm->thresh = thresh;
  42.     fbm->octaves = octaves;
  43.     fbm->omega = pow(lambda, (-0.5)*fbm->beta);
  44.     if (mapname != (char *)NULL)
  45.         fbm->colormap = ColormapRead(mapname);
  46.     else
  47.         fbm->colormap = (Color *)NULL;
  48.     return fbm;
  49. }
  50.  
  51. void
  52. FBmApply(fbm, prim, ray, pos, norm, gnorm, surf)
  53. FBm *fbm;
  54. Geom *prim;
  55. Ray *ray;
  56. Vector *pos, *norm, *gnorm;
  57. Surface *surf;
  58. {
  59.     Float val;
  60.     int index;
  61.  
  62.     val = fBm(pos, fbm->omega, fbm->lambda, fbm->octaves);
  63.     if (val < fbm->thresh)
  64.         val = fbm->offset;
  65.     else
  66.         val = fbm->offset + fbm->scale*(val - fbm->thresh);
  67.     if (fbm->colormap) {
  68.         index = 255. * val;
  69.         if (index > 255) index = 255;
  70.         if (index < 0) index = 0;
  71.         surf->diff.r *= fbm->colormap[index].r;
  72.         surf->diff.g *= fbm->colormap[index].g;
  73.         surf->diff.b *= fbm->colormap[index].b;
  74.         surf->amb.r *= fbm->colormap[index].r;
  75.         surf->amb.g *= fbm->colormap[index].g;
  76.         surf->amb.b *= fbm->colormap[index].b;
  77.     } else {
  78.         ColorScale(val, surf->diff, &surf->diff);
  79.         ColorScale(val, surf->amb, &surf->amb);
  80.     }
  81. }
  82.